var $ = null;
var currentWindowWidth = null;
var $form = null;
var playerImages = new Array();
var str = [" ", "é", "í", "ó", "á", "ý", "ú"];
var rep = ["-", "e", "i", "o", "a", "y", "u"];

jQuery(function() {
   $ = jQuery;
   
   currentWindowWidth = $(window).width();

   $form = $("#findBootsByPlayer");
   var $button = $form.find('[type=button]');

   $button.on("click", findBootByPlayer); 
   //console.log(playersBoots);
   //if($('#findBootsByPlayer').length > 0)
   	//findBootByPlayer();
});

function findBootByPlayer(e)
{
   var searchString = e != undefined ? $($(e.target).parent('form').find('input[type=search]')).val().toLowerCase() : 'n';
   //alert(searchString);
   var matches = new Array();

   for(var i in playersBoots)
   {
      if(playersBoots[i].name.toLowerCase().indexOf(searchString) > -1)      
         matches.push(playersBoots[i]);
   }
//   console.log(matches);
   renderResults(matches);
}

function renderResults(m)
{
	if(m.length > 0) {
		var $resultWrapper = $('#results');
			$resultWrapper.empty();

		for(i in m)
		{
			var $bootWrapper = $('<div class="boot"></div>');
            $resultWrapper.append($bootWrapper);
            $bootWrapper.append('<span>' + m[i].boot + '</span>');
            $bootWrapper.append('<span>' + m[i].name + '</span>');
			var newBootImage = document.createElement("img");
            newBootImage.src = m[i].bootUrl;
			$bootWrapper.append(newBootImage);

			$bootWrapper.append('<a class="best-price-btn" href="' + m[i].ffUrl + '">Se priser</a>');
			$bootWrapper.append('<a class="best-price-btn" href="' + m[i].priceUrl + '">Køb nu</a>');

			var newPlayerImage = document.createElement("img");
            newPlayerImage.src = m[i].playerUrl;
			$bootWrapper.append(newPlayerImage);

			var replacerClub = m[i].club.replaceArray(str, rep);
			var replacerNation = m[i].nation.replaceArray(str, rep);

			var $uList = $('<ul class="player-info-list"></ul>');
			$bootWrapper.append($uList);
			$uList.append('<li class="' + replacerClub.toLowerCase() + '">' + m[i].club + '</li>');
			$uList.append('<li class="position">' + m[i].position + '</li>');
			$uList.append('<li class="' + replacerNation.toLowerCase() + '">' + m[i].nation + '</li>');
			$uList.append('<li class="foot">' + m[i].foot + '</li>');
			$uList.append('<li class="age">' + m[i].age + '</li>');

			if(m[i].fClubs.trim().length > 0) {			
				var $fClubsWrapper = $('<ul class="former-clubs-list"></ul>');
				$bootWrapper.append($fClubsWrapper);
				var strSplit = m[i].fClubs;
				var resSplit = strSplit.split(", ");

				for(index in resSplit)
				{
					$fClubsWrapper.append('<li>' + resSplit[index] + '</li>');
				}
			}
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

String.prototype.replaceArray = function(find, replace) {
  var replaceString = this;
  for (var i = 0; i < find.length; i++) {
    replaceString = replaceString.replace(find[i], replace[i]);
  }
  return replaceString;
};